<?php
namespace Home\Controller;
use Home\Controller;
class ChatController extends BaseController {
	
	public function index(){
		$id=$_SESSION['username'];
		$this->assign('uid',$id);
		$this->display();
	}
	public function chat(){
		$res = M('chat')->select();
		$this->assign('res',$res);
		$this->display();
	}
public function admin(){
	    if(!isset($_SESSION['username'])){
		   $this->error('请先登录',U('User/login'));
		};
		$id=$_SESSION['username'];
		$this->assign('uid',$id);
		$this->display();
	}
	public function add(){
		M('chat')->add(I('post.'));
        $id=M('chat')->getLastInsID();
		$datas['cid']=$id;
		$data=array();
		$da = M('chat')->where($datas)->find();
		$this->ajaxReturn($da);
	}

	public function query(){
        $uid =$_SESSION['username'];
	
		$count = I('count');	
		$da=array(
		'uid'=>'admin'
		);
		$db=array(
		'resid'=>$uid
		);

		$s=0;
		while(true){
			sleep(1);
			$s++;
			if($s==20){
				$data = array(
					'success'=>false,
					'count'=>$count,	
				);
				$this->ajaxReturn($data);

				exit;
			}
			
			if($count<M('chat')->where($da)->where($db)->count()){
				$res = M('chat')->where($da)->where($db)->order('cid DESC')->limit(1)->select();
				$data = array(
					'success'=>true,
					'count'=>M('chat')->where($da)->where($db)->count(),
					'rows'=>$res		
				);
				$this->ajaxReturn($data);
			}			
		
		}
		
	
	}

	public function querys(){
        $resid=$_SESSION['username'];
	    $uid = I('uid');	
		$count = I('count');	
		$da=array(
		'uid'=>$uid
		);
		$db=array(
		'resid'=>$resid
		);

		$s=0;
		while(true){
			sleep(1);
			$s++;
			if($s==20){
				$data = array(
					'success'=>false,
					'count'=>$count,	
				);
				$this->ajaxReturn($data);

				exit;
			}
			
			if($count<M('chat')->where($da)->where($db)->count()){
				$res = M('chat')->where($da)->where($db)->order('cid DESC')->limit(1)->select();
				$data = array(
					'success'=>true,
					'count'=>M('chat')->where($da)->where($db)->count(),
					'rows'=>$res		
				);
				$this->ajaxReturn($data);
			}			
		
		}
		
	
	}


public function queryadmin(){
        $uid =$_SESSION['username'];
		$count = I('count');	
		
		
		$db=array(
		'resid'=>$uid
		);
		
		$s=0;
		while(true){
			sleep(1);
			$s++;
			if($s==20){
				$data = array(
					'success'=>false,
					'count'=>$count,	
				);
				$this->ajaxReturn($data);

				exit;
			}
			
			if($count<M('chat')->where($db)->count()){
				$res = M('chat')->where($db)->order('cid DESC')->limit(1)->find();
				$data = array(
					'success'=>true,
					'count'=>M('chat')->where($db)->count(),
					'rows'=>$res		
				);
				$this->ajaxReturn($data);
			}			
		
		}
		
	
	}







	public function read(){
		$count=I('count');
		$s = 0;
		while(true){

			usleep(500000);
			$s++;
			if($s>=10){
				echo '{"success":false}';
				exit();
			}else{
				
				if($count<M('chat')->count()){
					$count=M('chat')->count();
					$start = $count-10;
					if ($count<10){
						$start = 0;	
					}else{
						$start = $count-10;
					}
					$data = M('chat')->limit("$start , 10")->select();				
					$res = array(
						'success'=>true,
						'content'=>$data,
						'count'=>$count,
					);			
					
					$this->ajaxReturn($res);
					exit();
				}
			}			
		
		}
		
	}
}

/*
	班风建设提议

	组织活动
	互相帮助
	互相学习

*/